<?php
class ServerfilesController extends AppController {

	var $name = 'Serverfiles';
	var $components = array('FileHandler');
	var $uses = null;
	
	var $root = "img";
	var $ignore = array( '.DS_Store', '.', '..' );
		
	function index($year = null, $month = null) {
		$this->pre_admin();
		$needs_update = false;
		if ( !preg_match("/^[0-9]{4}$/", $year) and !preg_match("/^[0-9]{4}$/", $year)) { 
			// null or dosent match, first check if we got a stored $view_url in Session
			if (!$this->Session->read('view_array')) {
				$year = date('Y');
				$month = date('m');
				$needs_update = true;
			}
		} else $needs_update = true; // Parameters match, write new $view_url to Session
		if ($needs_update) $this->Session->write('view_array', array($year,$month) );
		
		// Set $prev_month and $next_month to view
		$view_array = $this->Session->read('view_array');

		$prev_month = mktime( 0, 0, 0, $view_array[1]-1, 1, $view_array[0] );
		$next_month = mktime( 0, 0, 0, $view_array[1]+1, 1, $view_array[0] );
		$this->set('prev_month', date('Y', $prev_month) . DS . date('m', $prev_month) );
		$this->set('next_month', date('Y', $next_month) . DS . date('m', $next_month) );
		$this->set('info', 'Displaying: ' . $view_array[1] . '/' . $view_array[0]);
		
		$view_url = 'posts' . DS . implode(DS, $view_array); // without 'img/' because the $html->img dont wants it
		$img_folder = $this->root . DS . $view_url; // for local php fileprocessing
		//echo $img_folder;
		// Check for uploaded file
		if(isset($this->params['form']['userfile'])) {
			if( !file_exists($img_folder) ) mkdir($img_folder);
            if ( $this->FileHandler->upload('userfile', $img_folder) ) {
				$this->set('result', 'Upload succeeded');
				$this->set('errorMessage', $this->FileHandler->getLastUploadData());
			} else {
				$this->set('result', 'Upload failed');
				$this->set('errorMessage', $this->FileHandler->errorMessage);
			}
			//
		}
		
		// Create files array;
		$files = array();
		if( file_exists($img_folder) ) {
			if( is_dir($img_folder) ) {
				$dh = @opendir( $img_folder ); 
		    	while( false !== ( $file = @readdir( $dh ) ) ){ 
		        	if (preg_match("/[.](jpg|gif|png)$/", $file)) { 
						$files []= array('title'=>$file, 'url'=>$view_url . DS . $file);
					} 
				} 
		    	@closedir( $dh );
			}
		} else {
			$this->set('result', "Upload a file to create a folder for this month.");
		}
		$this->set('files', $files);
		$this->layout = "plain";
	}
	/*
	function index($nxt_dir = null) {
		if(!$path = $this->Session->read('path')) {
			$path = array($this->root);
		} else {
			$path = $this->Session->read('path');
			if($nxt_dir) {
				//$tmp_path_array = array();
				//$tmp_path_array = explode("/", $path);
				//var_dump($tmp_path_array);
				
				if($nxt_dir == 'reset') $path = array($this->root);
				if($nxt_dir == 'up' and count($path)>1) array_pop($path);
				else if(file_exists(implode("/", $path) . "/$nxt_dir")) {
					$path []= $nxt_dir;
				} 
			}
		}
		if (!empty($this->params['form'])) {
			// Do we got a file upload..
			$result = "";
			if(isset($this->params['form']['new_file'])) {
				$this->set('debug', $this->params['form'] );
	            if ($this->FileHandler->upload('userfile', implode("/", $path))) {
					$result = 'upload succeeded';
					$this->set('uploadData', $this->FileHandler->getLastUploadData());
				} else {
					$result = 'Upload failed';
				}
				//$this->set('errorMessage', $this->FileHandler->errorMessage);
			} else if ( isset($this->params['form']['new_folder']) ) {
				if ($this->params['form']['folder_name'] != "") {
					$folder_name = implode("/", $path) . "/" . $this->params['form']['folder_name'];
					@mkdir($folder_name, 0755);
					$this->redirect('/serverfiles/index/');
				} else {
					$result = "New folder failed";
				}
			}
			
			$this->set('result', $result );
			
		}
		
		
		$files = $dirs = array();
		if(count($path)>1) $dirs []= "up";
		
	    $dh = @opendir( implode("/", $path) ); 
	    while( false !== ( $file = @readdir( $dh ) ) ){ 
	        if( !in_array( $file, $this->ignore ) ){ 
	            if( is_dir( implode("/", $path) . "/$file" ) ){ 
					$dirs []=$file;
	            } else if (preg_match("/[.](jpg|gif|png)$/", $file)) { 
	                $files []=$file;
	            } 
	        } 
	    } 
	    @closedir( $dh );
		$this->Session->write('path', $path); 
		$this->set('dirs', $dirs);
		$this->set('files', $files);
		$this->set('path', implode("/", $path));
	}
	*/
	
}